﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PTP.Usb;
using System.IO;

namespace PTP.Mock
{
  public class MockPipe : IPipe
  {
    protected bool IsInPipe;

    protected MemoryStream bytes;

    public MockPipe()
    {
      this.IsInPipe = false;
      this.bytes = new MemoryStream();
    }

    public MockPipe(byte[] data)
    {
      this.IsInPipe = true;
      this.bytes = new MemoryStream(data);
    }

    public byte[] GetSentData(bool clear)
    {
      byte[] result = this.bytes.ToArray();
      if (clear)
      {
        this.bytes.Dispose();
        this.bytes = new MemoryStream();
      }
      return result;
    }

    #region IPipe Members

    public int TransferData(byte[] buffer, int offset, int count)
    {
      if (this.IsInPipe)
      {
        return this.bytes.Read(buffer, offset, count);
      }
      else
      {
        this.bytes.Write(buffer, offset, count);
        return count;
      }
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
      this.bytes.Dispose();
      this.bytes = null;
    }

    #endregion
  }

  public class MockPipeStream : PipeStream
  {
    protected int bufferSize;

    public MockPipeStream(byte[] incomingData)
      : this(incomingData, 64)
    {

    }
    public MockPipeStream(byte[] incomingData, int bufferSize)
      : base(new MockPipe(incomingData), new MockPipe())
    {
      this.bufferSize = bufferSize;
    }


    public byte[] GetSentData(bool clear)
    {
      return ((MockPipe)this.outPipe).GetSentData(clear);
    }

    protected override int getBufferSize()
    {
      return bufferSize;
    }


  }
}
